/*
 * Created by Ben Kahn on 3/9/09
 */

package client.engine;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.util.Properties;

import server.LogException;
import server.Preferences;
import server.Status;

import communication.AbandonConversationRequest;
import communication.AcceptFileTransferRequest;
import communication.AcceptNewBuddyRequest;
import communication.AddNewBuddyRequest;
import communication.AddNewGroupRequest;
import communication.ClientConnection;
import communication.InitFileTransferRequest;
import communication.InviteBuddyChatRoomRequest;
import communication.NewAccountRequest;
import communication.OpenConversationRequest;
import communication.RejectNewBuddyRequest;
import communication.RemoveBuddyFromGroupRequest;
import communication.RemoveGroupRequest;
import communication.Request;
import communication.SendMessageRequest;
import communication.SignOffRequest;
import communication.SignOnRequest;
import communication.StatusRequest;
import communication.StillOnlineRequest;
import communication.StoreNewPreferencesRequest;

public class RequestProducer {
	
	private static final String properties = "client.properties";
	private ClientConnection clientConnection;
	private InetAddress localhost;
	private InetAddress hostAddress;
	private int incomingPort = 1234;
	private int outgoingPort = 44444;
	
	public RequestProducer ()
	{
		try {
			Properties prop = new Properties();
			InputStream file = null;
			
			try{
				file = new FileInputStream(properties);
				prop.load(file);
			}catch(Exception e){
				LogException.getInstance().logException(e);
			}
			
			if(prop != null){
				byte b1 = (byte) Integer.parseInt(prop.getProperty("byte1"));
				byte b2 = (byte) Integer.parseInt(prop.getProperty("byte2"));
				byte b3 = (byte) Integer.parseInt(prop.getProperty("byte3"));
				byte b4 = (byte) Integer.parseInt(prop.getProperty("byte4"));
				byte[] addressArray = new byte[]{b1,b2,b3,b4};
				
				incomingPort = Integer.parseInt(prop.getProperty("incoming"));
				outgoingPort = Integer.parseInt(prop.getProperty("outgoing"));
				
				localhost = InetAddress.getLocalHost();
				hostAddress = InetAddress.getByAddress(addressArray);
			}else{
				byte[] addressArray = new byte[]{(byte)169, (byte)234, (byte)111, (byte)22};
				localhost = InetAddress.getLocalHost();
				hostAddress = InetAddress.getByAddress(addressArray);
			}
			
			clientConnection = new ClientConnection(hostAddress, outgoingPort);
		}
		catch(IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public void sendRequest(Request r)
	{
		try
		{
			clientConnection = new ClientConnection(hostAddress, outgoingPort);
			clientConnection.writeRequest(r);
			clientConnection.close();
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public void sendNewAccountRequest(String username, String password)
	{
			NewAccountRequest r = new NewAccountRequest(username, password, localhost, incomingPort);
			sendRequest(r);
	}
	
	public void sendSignOnRequest(String username, String password)
	{
			SignOnRequest r = new SignOnRequest(username, password, localhost, incomingPort);
			sendRequest(r);
	}
	
	public void sendSignOffRequest(String username)
	{
		SignOffRequest r = new SignOffRequest(username);
		sendRequest(r);
	}
	
	public void sendSendMessageRequest(String sender, String receiver, String message)
	{
		SendMessageRequest r = new SendMessageRequest(sender, receiver, message);
		sendRequest(r);
	}
	
	public void sendStillOnlineRequest(String sender)
	{
		StillOnlineRequest r = new StillOnlineRequest(sender);
		sendRequest(r);
	}
	
	public void sendOpenConversationRequest(String sender, String buddy)
	{
		OpenConversationRequest r = new OpenConversationRequest(sender, buddy);
		sendRequest(r);
	}
	
	public void sendAbandonConversationRequest(String sender, String id)
	{
		AbandonConversationRequest r = new AbandonConversationRequest(sender, id);
		sendRequest(r);
	}
	
	public void sendInviteBuddyChatRoomRequest(String sender, String buddy, String id)
	{
		InviteBuddyChatRoomRequest r = new InviteBuddyChatRoomRequest(sender, buddy, id);
		sendRequest(r);
	}
	
	public void sendStatusRequest(String sender, String message, Status s)
	{
		StatusRequest r = new StatusRequest(sender, message, s);
		sendRequest(r);
	}
	
	public void sendAddNewBuddyRequest(String sender, String groupName, String buddyName)
	{
		AddNewBuddyRequest r = new AddNewBuddyRequest(sender, groupName, buddyName);
		sendRequest(r);
	}
	
	public void sendAddNewGroupRequest(String sender, String groupName)
	{
		AddNewGroupRequest r = new AddNewGroupRequest(sender, groupName);
		sendRequest(r);
	}
	
	public void sendAcceptNewBuddyRequest(String sender, String groupName, String receiver)
	{
		AcceptNewBuddyRequest r = new AcceptNewBuddyRequest(sender, groupName, receiver);
		sendRequest(r);
	}
	
	public void sendRejectNewBuddyRequest(String sender, String receiver)
	{
		RejectNewBuddyRequest r = new RejectNewBuddyRequest(sender, receiver);
		sendRequest(r);
	}
	
	public void sendRemoveBuddyFromGroupRequest(String sender, String groupName, String buddyName)
	{
		RemoveBuddyFromGroupRequest r = new RemoveBuddyFromGroupRequest(sender, groupName, buddyName);
		sendRequest(r);
	}
	
	public void sendRemoveGroupRequest(String sender, String groupName)
	{
		RemoveGroupRequest r = new RemoveGroupRequest(sender, groupName);
		sendRequest(r);
	}
	
	public void sendStoreNewPreferencesRequest(String sender, Preferences p)
	{
		StoreNewPreferencesRequest r = new StoreNewPreferencesRequest(sender, p);
		sendRequest(r);
	}
	
	public String getLocalHost()
	{
		return localhost.getHostAddress();
	}

	public void sendFileTransfer(String sender, String receiver, String path, String name){
		
		try{
			InitFileTransferRequest r = new InitFileTransferRequest(sender, path, name, receiver, InetAddress.getLocalHost(), incomingPort);
			sendRequest(r);
		}catch(Exception e){
			
		}
		
	}
	
	public void confirmFileTransfer(String sender, String receiver, String path, String file, InetAddress receiverIP, int port){
		
		AcceptFileTransferRequest r = new AcceptFileTransferRequest(receiver, sender, path, receiverIP, port);
		sendRequest(r);
		
	}
}
